package com.ep.commonbaseadapter.base;

import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/7/31.
 */

public class ViewHolder {
    private SparseArray<View> mViews;
    private View mConverView;

    public ViewHolder(View converView) {
        mConverView = converView;
        mViews=new SparseArray<>();

    }


    /**
     * 根据id获取控件
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View>T getView(int viewId){
        View view=mViews.get(viewId);
        if (view==null){
            view=mConverView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T) view;
    }

    /**
     * 设置文本
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId,String text){
        TextView view=getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 设置图片资源
     * @param viewId
     * @param resId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int resId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(resId);
        return this;
    }
}
